package com.zhj.agent.interceptor.httpservlet;

import com.zhj.agent.support.AbstractClassEnhancePluginDefine;
import com.zhj.agent.support.InstanceMethodsInterceptPoint;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;

import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;

public class HttpServletInstrumentation extends AbstractClassEnhancePluginDefine {

    private static final String ENHANCE_CLASS = "org.springframework.web.servlet.DispatcherServlet";

    private static final String DO_FILTER_METHOD = "service";

    private static final Class<HttpServletInterceptor> DO_FILTER_INTERCEPTOR = HttpServletInterceptor.class;

    public HttpServletInstrumentation() {
        super(ENHANCE_CLASS);
    }

    /**
     * Enhance instance
     *
     * @return the instance methods intercept point [ ]
     * @since 1.0.0
     */
    @Override
    public InstanceMethodsInterceptPoint[] enhanceInstance() {
        return new InstanceMethodsInterceptPoint[] {
            new InstanceMethodsInterceptPoint() {
                @Override
                public ElementMatcher<MethodDescription> getMethodsMatcher() {
                    //拦截对应的方法，并且是public方法
                    return named(DO_FILTER_METHOD).and(isPublic());
                }

                @Override
                public Class<?> getMethodsInterceptor() {
                    return DO_FILTER_INTERCEPTOR;
                }

                @Override
                public boolean isOverrideArgs() {
                    return false;
                }
            }
        };
    }
}
